var messageDelay = 2000;  // How long to display status messages (in milliseconds)

// Init the form once the document is ready
$( init );

// Initialize the form

function initAddItem() {

  // Hide the form initially.
  // Make submitForm() the form's submit handler.
  // Position the form so it sits in the centre of the browser window.
  $('#addItemWrapper').hide().addClass( 'positioned' );
  $('#addItemForm').submit( submitAddItemForm );

  // When the "Send us feedback" link is clicked:
  // 1. Fade the content out
  // 2. Display the form
  // 3. Move focus to the first field
  // 4. Prevent the link being followed

  $('a[href="#addItemForm"]').click( function() {
    $('#addItemWrapper').fadeIn( 'slow', function() {
      $('#itemDescription').focus();
    } )

    return false;
  } );
  
  // When the "Cancel" button is clicked, close the form
  $('#cancelAddItem').click( function() { 
    $('#addItemWrapper').fadeOut();
  } );  

  // When the "Escape" key is pressed, close the form
  $('#addItemForm').keydown( function( event ) {
    if ( event.which == 27 ) {
      $('#addItemWrapper').fadeOut();
    }
  } );

}


// Submit the form via Ajax

function submitAddItemForm() {
  var addItemForm = $(this);

  // Are all the fields filled in?

  if ($('#itemDescription').val().trim() == '') {

    // No; display a warning message and return to the form
    $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();
    addItemForm.fadeOut().delay(messageDelay).fadeIn();

  } else {

    // Yes; submit the form via Ajax
	//Prepare url + params
	var url = '/application/addActionItem/' + $('#planId').val().trim();
	
	/*if(($('#entityId').val().trim() != '')) url += '/' + entityId;
	else url += '0';
	
	if(($('#topicId').val().trim() != '')) url += '/' + topicId;
	else url += '0';*/	
	
	url += '/-1/-1';
	
	url += '/' + document.addItemForm.itemDescription.value;
	
	if(($('#stDate').val().trim() != '') && ($('#endDate').val().trim() != '')) url += '/' + $('#stDate').val().trim() + '/' + $('#endDate').val().trim();
	else url += '/0/0';
	
    $('#sendingMessage').fadeIn();
    
    addItemForm.fadeOut();
    
    alert(url);
    
    $.post(url , function(data){submitAddItemFinished("success"); });
     
  }
  // Prevent the default form submission occurring
  return false;
}


// Handle the Ajax response

function submitAddItemFinished( response ) {
  response = $.trim( response );
  $('#sendingMessage').delay(2000).fadeOut();
 
  if ( response == "success" ) {

    // Form submitted successfully
    	
    // 1. Display the success message
    // 2. Clear the form fields
    // 3. Fade the content back in

    $('#successMessage').fadeIn().delay(messageDelay).fadeOut();
    $('#itemDescription').val( "" );
    $('#stDate').val( "" );
    $('#endDate').val( "" );

  } else {

    // Form submission failed: Display the failure message,
    // then redisplay the form
    $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
    $('#addItemForm').delay(messageDelay+500).fadeIn();
  }
}